<?php
namespace Reseller\Controller;
use Think\Controller;
class WithdrawController extends IndexController {

	public function _initialize(){
		parent::_initialize();	
	 	$this->model="affiliateWithdraw"; //表名
	 	$this->key="aw_id";//排序键		 
	 	$this->title_index=L('withdraw');//选项卡标题	
	}
	
	public function index(){			
		$mist_arr=array();
		$accute_arr=array();
		$cookie_arr=array();	
		$con=searchCon($mist_arr,$accute_arr,$cookie_arr);
		$con['wd_mid']=$this->merchant_info['mh_id'];		
		parent::home($con);
	}
	
	//提现操作
	public function process(){
		//dump($this->reseller_info);exit;
		$this->reseller_info["aff_withdraw_status"] || $this->error(L("withdraw_forbidden"));
		I('wd_money')>=$this->reseller_info['aff_min_withdraw'] || $this->error(L("min_withdraw_limit"));
		I('wd_money')<=$this->reseller_info['aff_balance'] || $this->error(L("withdraw_amount_limit"));

		$oop=M($this->model);
		//开启事务
		$oop->startTrans();
		$data['aw_aff_id']=$this->reseller_info["aff_id"];
		$data['aw_amount']=I("wd_money");
		$data['aw_fee']=$this->reseller_info['aff_withdraw_fee'];
		$result=$oop->add($data);
		//写入对账单，并且修改账户余额
		$result1= resellerStatement($this->reseller_info['aff_id'],5,I("wd_money"),
		"Reseller withdraw money,withdraw #:".$result,$this->reseller_info['aff_withdraw_fee']);
		
		if($result && $result1){
			$oop->commit();//成功则提交
			$this->success(L("withdraw_success"));
		}else{
			$oop->rollback();//不成功则回滚
			$this->error(L("withdraw_failed"));
		}
	}
	
}

